package com.stylefeng.guns.modular.assetRegistration.controller;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.aop.Platform;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.BaseModelWarpper;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.assetRegistration.service.IAssetRegistrationService;
import com.stylefeng.guns.modular.system.model.AssetRegistration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 固定资产备查登记表控制器
 *
 * @author fengshuonan
 * @Date 2018-07-19 15:10:50
 * @author laoyu
 */
@Controller
@RequestMapping("/assetRegistration")
public class AssetRegistrationController extends BaseController {

    private String PREFIX = "/assetRegistration/assetRegistration/";

    @Autowired
    private IAssetRegistrationService assetRegistrationService;

    /**
     * 跳转到固定资产备查登记表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "assetRegistration.html";
    }

    /**
     * 跳转到添加固定资产备查登记表
     */
    @RequestMapping("/assetRegistration_add")
    public String assetRegistrationAdd() {
        return PREFIX + "assetRegistration_add.html";
    }

    /**
     * 跳转到修改固定资产备查登记表
     */
    @RequestMapping("/assetRegistration_update/{assetRegistrationId}")
    public String assetRegistrationUpdate(@PathVariable Integer assetRegistrationId, Model model)  {
        AssetRegistration assetRegistration = assetRegistrationService.selectById(assetRegistrationId);
        model.addAttribute("item",assetRegistration);
        LogObjectHolder.me().set(assetRegistration);
        return PREFIX + "assetRegistration_edit.html";
    }

    /**
     * 获取固定资产备查登记表列表
     * @Date 2018-07-24
     * @Author laoyu
     *
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name,@RequestParam(required = false) String coding,
                       @RequestParam(required = false) String specification,@Platform String platform)throws Exception {

        Page<AssetRegistration> page = new PageFactory<AssetRegistration>().defaultPage();
        EntityWrapper<AssetRegistration> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(name))
            wrapper.eq("name",name);
        if (StringUtils.isNotBlank(coding))
            wrapper.eq("coding",coding);
        if (StringUtils.isNotBlank(specification))
            wrapper.eq("specification",specification);
        if ("gh".equals(ShiroKit.getUser().getDeptcode())&&StringUtils.isNotBlank(platform)) {
            wrapper.eq("platform", platform);
        }else {
            wrapper.eq("platform",ShiroKit.getUser().getDeptcode());
        }
        wrapper.orderBy("purchase_time",false);

        Page<AssetRegistration> assetRegistrationPage = assetRegistrationService.selectPage(page,wrapper);
        //填充字典类型
        assetRegistrationPage.setRecords((List<AssetRegistration>)new BaseModelWarpper(assetRegistrationPage.getRecords(),AssetRegistration.class).warpToObj());
        return  super.packForBT(assetRegistrationPage);
    }

    /**
     * 新增固定资产备查登记表
     *
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(AssetRegistration assetRegistration) {
        assetRegistrationService.insert(assetRegistration);
        return SUCCESS_TIP;
    }

    /**
     * 删除固定资产备查登记表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer assetRegistrationId) {
        assetRegistrationService.deleteById(assetRegistrationId);
        return SUCCESS_TIP;
    }

    /**
     * 修改固定资产备查登记表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(AssetRegistration assetRegistration) {
        assetRegistrationService.updateById(assetRegistration);
        return SUCCESS_TIP;
    }

    /**
     * 固定资产备查登记表详情
     * @Date 2018-07-24
     *  @Author laoyu
     */
    @RequestMapping(value = "/detail/{assetRegistrationId}")
    @ResponseBody
    public Object detail(@PathVariable("assetRegistrationId") Integer assetRegistrationId) {
        return assetRegistrationService.selectById(assetRegistrationId);
    }

    @RequestMapping(value = "/tatol")
    @ResponseBody
    public Object total(@RequestParam(required = false) String name,@RequestParam(required = false) String coding,
                        @RequestParam(required = false) String specification,@Platform String platform) throws Exception {
        //初始化日期
        EntityWrapper<AssetRegistration> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(name))
            wrapper.eq("name",name);
        if (StringUtils.isNotBlank(coding))
            wrapper.eq("coding",coding);
        if (StringUtils.isNotBlank(specification))
            wrapper.eq("specification",specification);
        if ("gh".equals(ShiroKit.getUser().getDeptcode())&&StringUtils.isNotBlank(platform)) {
            wrapper.eq("platform", platform);
        }else {
            wrapper.eq("platform",ShiroKit.getUser().getDeptcode());
        }
        List<AssetRegistration> assetRegistrationList = assetRegistrationService.selectList(wrapper);
        int num = 0;//数量
        double monery = 0;//金额
        for (AssetRegistration assetRegistration : assetRegistrationList){
//            num += assetRegistration.getQuantity();
            monery += assetRegistration.getAmount();
        }
        Map<String,Object> result = new HashMap<>();
//        result.put("num",num);
        result.put("monery",monery);
        return result;
    }

    /**
     * 描述:导出固定资产登记表
     * 创建人: laoyu
     * 日期: 2018/8/6 0006
     * 时间: 下午 5:04
     */
    @RequestMapping("/load")
    public void downloadByPoiBaseView(ModelMap modelMap, HttpServletRequest request,HttpServletResponse response,
                                      @RequestParam(required = false) String name,@RequestParam(required = false) String coding,
                                      @RequestParam(required = false) String specification,@Platform String platform) throws Exception  {


        //查询要导出工资
        EntityWrapper<AssetRegistration> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(name))
            wrapper.eq("name",name);
        if (StringUtils.isNotBlank(coding))
            wrapper.eq("coding",coding);
        if (StringUtils.isNotBlank(specification))
            wrapper.eq("specification",specification);
        if ("gh".equals(ShiroKit.getUser().getDeptcode())&&StringUtils.isNotBlank(platform)) {
            wrapper.eq("platform", platform);
        }else {
            wrapper.eq("platform",ShiroKit.getUser().getDeptcode());
        }
        List<AssetRegistration> assetRegistrationList = assetRegistrationService.selectList(wrapper);

        //初始化导出的模板表格
        TemplateExportParams params = new TemplateExportParams(
                "exceldoc/assetRegistration.xlsx");

        modelMap.put(TemplateExcelConstants.FILE_NAME, platform.toUpperCase()+"固定资产登记表");
        modelMap.put(TemplateExcelConstants.PARAMS, params);
        modelMap.put(TemplateExcelConstants.MAP_DATA,  assetRegistrationService.load(assetRegistrationList));
        PoiBaseView.render(modelMap, request, response,
                TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);

    }
}
